package com.mohe.dangjian.action;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;

import com.mohe.dangjian.model.Role;
import com.mohe.dangjian.service.interfaces.IRole;
import com.mohe.dangjian.utils.Json;
import com.mohe.dangjian.utils.JsonUtil;
import com.mohe.dangjian.utils.Page;
import com.opensymphony.xwork2.ActionSupport;

public class RoleAction extends ActionSupport {

	private static final long serialVersionUID = 6267896827346331881L;
	
	private IRole roleServ;
	
	private List<Role> roleList;
	private Role role = new Role();
	private Page page = new Page();
	
	// 页面提示
	private String flag;
	
	public IRole getRoleServ() {
		return roleServ;
	}
	public void setRoleServ(IRole roleServ) {
		this.roleServ = roleServ;
	}
	public List<Role> getRoleList() {
		return roleList;
	}
	public void setRoleList(List<Role> roleList) {
		this.roleList = roleList;
	}
	public Role getRole() {
		return role;
	}
	public void setRole(Role role) {
		this.role = role;
	}
	public Page getPage() {
		return page;
	}
	public void setPage(Page page) {
		this.page = page;
	}
	public String getFlag() {
		return flag;
	}
	public void setFlag(String flag) {
		this.flag = flag;
	}
	
	public String getRoleListForPage(){
		int count = roleServ.getCount(role);
		page.setCount(count);
		roleList = roleServ.getRoleList(role, page);
		return "role.jsp";
	}
	
	/**
	 * 分页
	 */
	public void searchRoleList() {
		Json json = new Json();
		List<Map<String, String>> mapList = new ArrayList<Map<String, String>>();
		int count = roleServ.getCount(role);
		page.setCount(count);
		List<Role> list = roleServ.getRoleList(role, page);
		if (list != null) {
			int index = (page.getCurrPage() - 1) * page.getPageSize() + 1;
			for (Role s : list) {
				Map<String, String> map = new HashMap<String, String>();
				map.put("index", index + "");
				if (s.getTimeKey() != null) {
					map.put("timeKey", s.getTimeKey() + "");
				} else {
					map.put("timeKey", "0");
				}
				if (!StringUtils.isBlank(s.getName())) {
					map.put("name", s.getName());
				} else {
					map.put("name", "");
				}
				mapList.add(map);
				index++;
			}
		}
		json.setObject(page);
		json.setMap(mapList);
		JsonUtil.writeJson(json);
	}
	
	/**
	 * 跳转到编辑界面
	 * @return
	 */
	public String toEditRole(){
		role = roleServ.getRoleById(role.getTimeKey());
		return "editRole.jsp";
	}
	
	/**
	 * 更新角色信息
	 */
	public void updateRole(){
		Json json = new Json();
		boolean result = roleServ.updateRole(role);
		if (result) {
			json.setMessage("编辑成功");
		} else {
			json.setSuccess(true);
			json.setMessage("编辑失败");
		}
		JsonUtil.writeJson(json);
	}
	
	/**
	 * 保存新增角色
	 */
	public void saveRole() {
		Json json = new Json();
		boolean result = roleServ.saveRole(role);
		if (result) {
			json.setMessage("新增成功");
		} else {
			json.setSuccess(true);
			json.setMessage("新增失败");
		}
		JsonUtil.writeJson(json);
	}
	
	/**
	 * 删除角色
	 */
	public void delRole() {
		Json json = new Json();
		boolean result = roleServ.delRole(role.getTimeKey());
		if (result) {
			json.setMessage("删除成功");
		} else {
			json.setSuccess(true);
			json.setMessage("删除失败");
		}
		JsonUtil.writeJson(json);
	}
}
